var createError = require('http-errors'); //第三方模块，处理错误
var express = require('express');//express框架
var path = require('path');//内置模块
var cookieParser = require('cookie-parser');//第三方模块
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
const productRouter=require('./routes/product');

var app = express();

// view engine setup
//设置输出引擎
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev')); //使用日志中间件
app.use(express.json());//处理body中的json数据，将数据结果放入req.body

app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/product',productRouter);

// catch 404 and forward to error handler
//处理404 创建一个错误处理中间件交给下一个中间件
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
 //渲染错误页面
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
